#coding:utf-8

from twisted.python import log as logger
from twisted.python import logfile
import sys, os
import time

#日志端
class DataReport(object):
    def __init__(self, directory):
        if not os.path.isdir(directory):
            os.mkdir(directory)
        self.directory = directory
        self.curFile = None
        self.lastDate = None
        
    def checkTime(self):
        now = time.strftime("%y%m%d%H")
        if now > self.lastDate:
            fullName = os.path.join(self.directory, '%s.log'%now)
            self.curFile = file(fullName, "a", 0 )
            self.lastDate = now

    def log(self, data):
        self.checkTime()
        if self.curFile:
            self.curFile.write(time.strftime("(%Y-%m-%d %H:%M:%S)")+data+ "\r\n")
            #self.curFile.flush()
        else:
            info("None Log File.")
        
_dataReport = None
log = None
if _dataReport:
    log = _dataReport.log
info = logger.msg
err = logger.err

def setLogDirectory(directory):
    global _dataReport, log
    if not os.path.isdir(directory):
            os.mkdir(directory)
    _dataReport = DataReport(directory)
    log = _dataReport.log
    logger.startLogging(logfile.DailyLogFile("now", "log"))
